﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using System.Data.SqlClient;
using AspNetCs.Common;
using System.Web;
using AspNetCs.Model;
using System.Data;
namespace AspNetCs.SQLServerDAL
{
    public class Admin_DAL: IAdmin
    {


        #region IAdmin 成员
        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AdminLogin(Admin_Model model)
        {
            int IsLogin = -1;
            db Conn = new db();
            try
            {
                SqlDataReader sdr = Conn.GetSqlDataReader("Select Top 1 * from Admin where UserName='" + model.UserName.Trim().ToLower() + "' and PassWord='" + EncryptString.GetEncrypt(model.Pwd.Trim().ToLower()) + "'");
                if (sdr.Read())
                {
                    IsLogin = 0;//用户名密码验证成功
                    UpdateAdminLast(sdr.GetInt32(0));
                    HttpContext.Current.Session["AdminInfo"] = GetModel(sdr.GetInt32(0));
                }

            }
            catch
            {
                IsLogin = -1;//用户名,密码错误
            }
            finally
            {
                Conn.close();
            }
            return IsLogin;



        }
        /// <summary>
        /// 根据DATAROW,返回管理员资料
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public Admin_Model GetModel(DataRow dr)
        {
            Admin_Model model = new Admin_Model();
            if (dr["ID"] != null) { model.Id = Convert.ToInt32(dr["ID"].ToString()); }
            if (dr["UserName"] != null) { model.UserName = dr["UserName"].ToString(); }
            if (dr["Password"] != null) { model.Pwd = dr["Password"].ToString(); }
            if (dr["Purview"] != null) { model.Purview = Convert.ToInt32(dr["Purview"].ToString()); }
            if (dr["LastLoginIP"] != null) { model.LastLoginIp = dr["LastLoginIP"].ToString(); }
            if (dr["LastLoginTime"] != null) { model.LastLoginTime = dr["LastLoginTime"].ToString(); }
            if (dr["LastLogoutTime"] != null) { model.LastLoginOutTime = dr["LastLogoutTime"].ToString(); }
            if (dr["LoginTimes"] != null) { model.LoginTimes = Convert.ToInt32(dr["LoginTimes"].ToString()); }
            return model;
        }

        #endregion


        /// <summary>
        /// 根据ID,返回管理员资料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Admin_Model GetModel(int id)
        {
            Admin_Model model = new Admin_Model();
            db Cn = new db();
            DataTable dt = Cn.GetSql_DataTable("Select Top 1 * from Admin  Where Id=" + id);
            if (dt.Rows.Count > 0)
            {
                model = GetModel(dt.Rows[0]);

            }
            return model;
        }


        /// <summary>
        /// 当管理员登录以后,更新管理员最后登录资料
        /// </summary>
        /// <param name="id"></param>
        private void UpdateAdminLast(int id)
        {
            db ConnUpdate = new db();
            string Sql = "Update Admin Set loginTimes=loginTimes+1,LastLoginTime='" + DateTime.Now + "',LastLoginIP='" + StringHelper.Ip() + "'  where id=" + id.ToString();
            ConnUpdate.Execute_Noget(Sql);
            ConnUpdate.close();
        }



    }
}
